package com.aioz.security.service.impl;

import com.aioz.security.common.ResponseResult;
import com.aioz.security.entity.Log;
import com.aioz.security.mapper.LogMapper;
import com.aioz.security.service.LogService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Aioz
 * @since 2022-11-18
 */
@Service
@Slf4j
public class LogServiceImpl extends ServiceImpl<LogMapper, Log> implements LogService {


    @Override
    public ResponseResult getLogsPage(Integer currentPage, Integer size) {
        Page<Log> page = new Page<>(currentPage,size);
        Page<Log> logPage = query().page(page);
        List<Log> logs = logPage.getRecords();
        long total = logPage.getTotal();
        Map<String,Object> map = new HashMap<>();
        map.put("logs",logs);
        map.put("total",total);
        return new ResponseResult<>(200,map);
    }

    @Override
    public ResponseResult getLogsByUserId(Integer userId) {
        List<Log> logs = query().eq("user_id", userId).list();
        logs = logs.stream().limit(10).collect(Collectors.toList());
        return new ResponseResult<>(200,logs);
    }

    @Override
    public ResponseResult getLogsByStatusCode(Integer statusCode) {
        List<Log> logs = query().eq("status_code", statusCode).list();
        logs = logs.stream().limit(10).collect(Collectors.toList());
        return new ResponseResult<>(200,logs);
    }

    @Override
    public ResponseResult deleteLog(Integer id) {
        log.info("{}",id);
        boolean isDelete = removeById(id);
        if(isDelete){
            return new ResponseResult<>(200,"删除成功！");
        }
        return new ResponseResult<>(200,"删除失败！");
    }
}
